package cn.iocoder.yudao.module.member.vip.controller.admin;

import cn.iocoder.yudao.framework.business.basic.controller.CrudController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.infrastructure.config.ConfigApiV2;
import cn.iocoder.yudao.module.api.infrastructure.config.dto.MemberConfigRespDTO;
import cn.iocoder.yudao.module.member.vip.dto.VipCardQueryDTO;
import cn.iocoder.yudao.module.member.vip.dto.VipCardSaveDTO;
import cn.iocoder.yudao.module.member.vip.service.VipCardService;
import cn.iocoder.yudao.module.member.vip.vo.VipCardVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.api.infrastructure.ConfigConstants.CONFIG_MEMBER;

@Tag(name = "管理后台 - 会员卡")
@RestController
@RequestMapping("/member/vip-card")
@Validated
public class VipCardController implements CrudController<VipCardVO, VipCardSaveDTO, VipCardQueryDTO> {
    @Resource
    private VipCardService vipCardService;

    @Resource
    private ConfigApiV2 configApiV2;

    @PreAuthorize("@ss.hasPermission('member:vip-card:create')")
    @Override
    public CommonResult<Long> create(VipCardSaveDTO param) {
        return success(vipCardService.create(param));
    }

    @PreAuthorize("@ss.hasPermission('member:vip-card:update')")
    @Override
    public CommonResult<Boolean> update(VipCardSaveDTO param) {
        return success(vipCardService.update(param));
    }

    @PreAuthorize("@ss.hasPermission('member:vip-card:delete')")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return success(vipCardService.delete(id));
    }

    @PreAuthorize("@ss.hasPermission('member:vip-card:query')")
    @Override
    public CommonResult<VipCardVO> getDetail(Long id) {
        return success(vipCardService.getDetail(id));
    }

    @PreAuthorize("@ss.hasPermission('member:vip-card:query')")
    @Override
    public CommonResult<PageResult<VipCardVO>> getPage(Integer pageNo, Integer pageSize, VipCardQueryDTO param) {
        PageResult<VipCardVO> page = vipCardService.getPage(pageNo, pageSize, param);
        int deductUnitPrice = configApiV2.getConfig(CONFIG_MEMBER, MemberConfigRespDTO.class).getPointTradeDeductUnitPrice();
        for (VipCardVO item : page.getList()) {
            item.setDeductPrice(deductUnitPrice * item.getGivePoint());
        }
        return success(page);
    }

}